
/*
 * Copyright 2008 Friendster Inc.
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 
 
 * Unless required by applicable law or agreed to in writing, 
 * software distributed under the License is distributed on an "AS IS" BASIS, 
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and limitations under the License.
*/

using System;
using System.Collections;
using System.Collections.Generic;
using System.Text;
using System.Xml;
using System.Xml.Schema;
using System.IO;
using System.Xml.XPath;

namespace Friendster.Service.Core
{
    internal sealed class XMLReader
    {
        private string _xmlToParse;
        private Hashtable _dataTable;
        private ArrayList _dataContainer;
        private ArrayList container = new ArrayList();
        private  long o =0;
        // Methods
        internal XMLReader(string xmlToParse)
        {
            this._xmlToParse = xmlToParse;
        }
        public XMLReader(long i)
        {
            o = i;
        }
        public  object ReadValue()
        {
            _dataTable = new Hashtable();
            _dataContainer = new ArrayList();
            XPathNavigator navigator;
            XPathDocument docNav;
            XmlTextReader reader = null;
            reader = new XmlTextReader(new StringReader(this._xmlToParse));
            docNav = new XPathDocument(reader);
            navigator = docNav.CreateNavigator();
            navigator.MoveToRoot();
            navigator.MoveToFirstChild();
            navigator.MoveToFirstChild();
            do
            {
                if (_dataTable.ContainsKey(navigator.Name))
                {
                  
                    _dataTable = new Hashtable();
                }
                
                if (navigator.HasChildren)
                {
                    if (navigator.InnerXml.StartsWith("<") && !navigator.InnerXml.StartsWith("<!["))
                    {
                        if (navigator.InnerXml.EndsWith(">") && !navigator.InnerXml.EndsWith("]]>"))
                        {
                            container.Clear();
                            _dataTable.Add(navigator.Name, ReadInnerValues(navigator.InnerXml));
                        }
                        else
                            _dataTable.Add(navigator.Name, navigator.Value);
                    }
                    else
                        _dataTable.Add(navigator.Name, navigator.Value);
                }
                else
                    _dataTable.Add(navigator.Name, navigator.Value);

                _dataContainer.Add((object)_dataTable);

            } while (navigator.MoveToNext());

            return (object)_dataContainer;
        }

        public object ReadInnerValues(string xml)
        {
            Hashtable table = new Hashtable();
           
            xml = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n<root>" + xml + "</root>";
            XPathNavigator _navigator;
            XmlDocument xd = new XmlDocument();
            xd.LoadXml(xml);
            _navigator = xd.CreateNavigator();
            _navigator.MoveToRoot();
            _navigator.MoveToFirstChild();
            _navigator.MoveToFirstChild();
            do
            {
                if (table.ContainsKey(_navigator.Name))
                {
                    container.Add((object)table[_navigator.Name]);
                    table.Clear();
                }
                if (_navigator.HasChildren)
                {
                    if (_navigator.InnerXml.StartsWith("<") && !_navigator.InnerXml.StartsWith("<!["))
                    {
                        if (_navigator.InnerXml.EndsWith(">") && !_navigator.InnerXml.EndsWith("]]>"))
                        {
                            
                            XMLReader nr = new XMLReader(o);
                            table.Add(_navigator.Name, nr.ReadInnerValues(_navigator.InnerXml));
                        }
                        else
                            table.Add(_navigator.Name, _navigator.Value);
                    }
                    else
                        table.Add(_navigator.Name, _navigator.Value);
                }
                else
                    table.Add(_navigator.Name, _navigator.Value);
            } while (_navigator.MoveToNext());
           
            if (table.Count == 1)
                container.Add((object)table[_navigator.Name]);
            else if (table.Count > 1 && container.Count>0)
                container.Add((object)table);

            if (container.Count > 0)
            {
                
                return (object)container;
            }
            else
                return (object)table;
        }

    }
}